package com.ship.contract.service.impl;

import com.ship.contract.bean.ConResourceMemorandumPosition;
import com.ship.contract.mapper.ConResourceMemorandumPositionMapper;
import com.ship.contract.service.ConResourceMemorandumPositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service("ConResourceMemorandumPositionService")
public class ConResourceMemorandumPositionImpl extends ContractBaseServiceImpl<ConResourceMemorandumPositionMapper, ConResourceMemorandumPosition> implements ConResourceMemorandumPositionService {

    @Autowired
    private ConResourceMemorandumPositionMapper conResourceMemorandumPositionMapper;

    @Override
    public void saveInfo(Map map, String filePath) {
        Map<String, Object> resultObject = (Map<String, Object>) map.get("contractData");
        if (resultObject != null) {
            for (String key : resultObject.keySet()) {
                if (key.endsWith("LocVal") && resultObject.get(key) instanceof Map) {
                    ConResourceMemorandumPosition position = new ConResourceMemorandumPosition();

                    position.setFile(filePath);
                    Map<String, Object> locData = (Map<String, Object>) resultObject.get(key);
                    if (locData.containsKey("x1")) {
                        position.setX1(new BigDecimal(locData.get("x1").toString()));
                    }
                    if (locData.containsKey("x2")) {
                        position.setX2(new BigDecimal(locData.get("x2").toString()));
                    }
                    if (locData.containsKey("y1")) {
                        position.setY1(new BigDecimal(locData.get("y1").toString()));
                    }
                    if (locData.containsKey("y2")) {
                        position.setY2(new BigDecimal(locData.get("y2").toString()));
                    }
                    if (locData.containsKey("page")) {
                        position.setPage(new BigDecimal(locData.get("page").toString()));
                    }
                    String nameKey = key.replace("LocVal", "Name");
                    if (resultObject.containsKey(nameKey)) {
                        String oldName = String.valueOf(resultObject.get(nameKey));
                        position.setOldName(oldName);
                    }
                    position.setFiledValue(String.valueOf(resultObject.get(key.replace("LocVal", "Val"))));
                    position.setFiledName(key.replace("Loc", ""));
                    super.save(position);
                }
            }
        }
    }

    @Override
    public List<ConResourceMemorandumPosition> getFieldLocation(Long conId) {
        return conResourceMemorandumPositionMapper.getFieldLocation(conId);
    }
}